package com.yxjx.duoxue.payment;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.AdapterView.OnItemClickListener;

import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.customview.YxListView;
import com.yxjx.duoxue.dao.Dao;
import com.yxjx.duoxue.global.Globals;
import com.yxjx.duoxue.network.UserLoginResponse;
import com.yxjx.duoxue.payment.OrderKidsConfigurationView.MyAdapter;
import com.yxjx.duoxue.utils.UIHelper;

public class KidInfoSelectLablesActivity extends BaseActionBarActivity {
	
	public static final String KEY_SELECT_LABELS = "KEY_SELECT_LABELS";
	private ArrayList<ConfigKidsDetailDTS> data;
	private ArrayList<Boolean> selectedStates;
	private YxListView mListView;
	private MyAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_kid_info_select_labels);
		customizeActionBarTitle("性格特征");
		mListView = (YxListView) findViewById(R.id.all_labels);
		
		adapter = new MyAdapter();
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				selectedStates.set(arg2, !selectedStates.get(arg2));
				adapter.notifyDataSetInvalidated();
			}
		});
		
		setDataFromAppDataModel();
	}
	
	private void setDataFromAppDataModel() {
		Globals appData = Globals.getInstance(this);
		if (appData.getKidOption() == null) {
			data = null;
		} else {
			if (appData.getKidOption().getKidsConfigList() != null) {
				data = (ArrayList<ConfigKidsDetailDTS>) appData.getKidOption().getKidsConfigList();
				// 初始化selectedStates
				selectedStates = new ArrayList<Boolean>(data.size());
				for (int i = 0; i < data.size(); i++) {
					selectedStates.add(false);
				}
			}
			
			ClientUserKidsDTS item = Dao.getUserKidInfo(getApplicationContext());
			if (item != null) {
				HashMap<Integer, Integer> kidsConfigMap = new HashMap<Integer, Integer>();
				for (int i = 0; i < data.size(); i++) {
					ConfigKidsDetailDTS itemConfig = data.get(i);
					kidsConfigMap.put(itemConfig.getId(), i);
				}
				
				// labels selected
//				String labels = item.getKidsDetail();
//				ArrayList<Integer> labelsInt = OrderKidsConfigurationView.str2IntegerArray(labels);
//				if (labelsInt != null) {
//					for (Integer label : labelsInt) {
//						selectedStates.set(kidsConfigMap.get(label), true);
//					}
//				}
//				
				ArrayList<ConfigKidsDetailDTS> selectedItems = (ArrayList<ConfigKidsDetailDTS>) getIntent()
						.getSerializableExtra(KEY_SELECT_LABELS);
				if (selectedItems != null) {
					for (ConfigKidsDetailDTS each : selectedItems) {
						selectedStates.set(kidsConfigMap.get(each.getId()), true);
					}
				}
			}
		}
	}
	
	
	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			int len = data == null ? 0 : data.size();
			return len;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret;
			if (arg1 == null) {
				ret = LayoutInflater.from(KidInfoSelectLablesActivity.this).inflate(R.layout.item_kid_config_list, null);
			} else {
				ret = arg1;
			}
			UIHelper.setText(ret, R.id.text, data.get(arg0).getKidsDes());
			ret.findViewById(R.id.text).setSelected(selectedStates.get(arg0));
			ret.findViewById(R.id.indicator).setSelected(selectedStates.get(arg0));
			
			return ret;
		}
	}
	
	public void selectLabels(View view) {
		ArrayList<ConfigKidsDetailDTS> items = getSelectedLables();
		if (items == null || items.size() < 1 || items.size() > 3) {
			UIHelper.showTips(mListView, "请选择1到3个最合适的特征");
			return;
		}
		//
		Intent intent = new Intent();
		intent.putExtra(KEY_SELECT_LABELS, items);
		setResult(RESULT_OK, intent);
		finish();
	}
	
	private ArrayList<ConfigKidsDetailDTS> getSelectedLables() {
		if (selectedStates == null || selectedStates.size() == 0 
				|| data == null || data.size() == 0) {
			return null;
		}
		
		ArrayList<ConfigKidsDetailDTS> items = new ArrayList<ConfigKidsDetailDTS>();
		for (int i = 0; i < data.size(); i++) {
			if (selectedStates.get(i)) {
				items.add(data.get(i));
			}
		}
		return items;
	}
}
